/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sessionbeans;

import entity.HashTag;
import entity.Tweet;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Axel
 */
@Stateless
public class TweetFacade extends AbstractFacade<Tweet> {
    @PersistenceContext(unitName = "20121106_TwitterM2iPU")
    private EntityManager em;
    
    @EJB
    HashTagFacade hf;

    protected EntityManager getEntityManager() {
        return em;
    }

    public TweetFacade() {
        super(Tweet.class);
    }

    public List<Tweet> listTweet(String label){
       
       HashTag h =  hf.getByName(label);
       
       if(null==h) return new ArrayList<Tweet>();
        
       Long hashID = h.getId();
      
       return  em.createQuery("select t from Tweet t inner join t.tags x where x.id = :hashTagId")
               .setParameter("hashTagId", hashID).getResultList();
       
           
    }
    
}
